/*
 * $Id: DefaultComponentFactory.java 1384 2008-09-14 00:05:09Z andrewinkler $
 * ============================================================================
 * Project groovy-example
 * Copyright (c) 2008 by Andre Winkler. All rights reserved.
 * ============================================================================
 *          GNU LESSER GENERAL PUBLIC LICENSE
 *  TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

package de.gluehloch.swinger.binding;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

class PropertyChangeSupportComposite {
    private final delegate;
    private final pcs = new PropertyChangeSupport(this)

    PropertyChangeSupportComposite(def _delegate) {
        delegate = _delegate
    }

    void setProperty(String name, value) {
        if (hasLocalProperty(name)) {
            this.@"$name" = value
        } else {
            def oldValue = getProperty(name)
            delegate.setProperty(name, value)
            pcs.firePropertyChange(name, oldValue, value)
        }
    }

    def getProperty(String name) {
        if (hasLocalProperty(name)) {
            return this.@"$name"
        } else {
            return delegate.getProperty(name)
        }
    }

    void addPropertyChangeListener(String key, PropertyChangeListener listener) {    
        pcs.addPropertyChangeListener(key, listener)
    }

    void addPropertyChangeListener(PropertyChangeListener listener) {
        pcs.addPropertyChangeListener(listener)
    }

    void removePropertyChangeListener(PropertyChangeListener listener) {
        pcs.removePropertyChangeListener(listener)
    }

    private hasLocalProperty(name) {
        metaClass.properties.collect{ it.name }.contains(name)
    }
    
}
